package chapter06;

public class Java04_Exception {
    public static void main(String[] args) {
        // TODO 异常
        // 3.索引越界:java.lang.ArrayIndexOutOfBoundsException
        // 运行期异常
        String[] names = new String[3];
        names[0] = "张三";
        names[1] = "李四";
        names[2] = "王五";
        if (names.length == 4) {
            System.out.println(names[3]);
        }
        /*
        for (int i = 0; i < 5; i++) {
            System.out.println(names[i]);
        }
        */
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }

        System.out.println("-----------------------");

        // 4.字符串索引越界:java.lang.StringIndexOutOfBoundsException
        String a = "abc";
        //System.out.println(a.charAt(3));
        System.out.println(a.substring(3));

        System.out.println("-----------------------");

        System.out.println(a.substring(4)); // subString()方法传入的值大于字符串索引长度时才会出现索引越界的异常
    }
}